package com.bw.zcb.fragment;


import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bw.zcb.R;
import com.bw.zcb.mvp.base.model.BaseEntity;
import com.bw.zcb.mvp.base.view.BaseFragment;
import com.bw.zcb.mvp.contract.UserContract;
import com.bw.zcb.mvp.di.DaggerUserComponent;
import com.bw.zcb.mvp.di.UserModules;
import com.bw.zcb.mvp.model.entity.UserEntity;
import com.bw.zcb.mvp.model.entity.UserItemEntity;
import com.bw.zcb.mvp.model.entity.UserMenuEntity;
import com.bw.zcb.mvp.presenter.UserPresenter;
import com.bw.zcb.mvp.view.user.LoginActivity;
import com.bw.zcb.mvp.view.user.PersonActivity;
import com.bw.zcb.mvp.view.user.adapter.UserAdapter;
import com.bw.zcb.mvp.view.user.adapter.UserMenuAdapter;
import com.bw.zcb.network.tool.App;
import com.bw.zcb.utils.OnLine;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public  class MineFragment extends BaseFragment<UserPresenter> implements UserContract.IUserView,View.OnClickListener {
    private View menuView, timeLineView;
    private RecyclerView user_rv;
    private RecyclerView menu_rv;

    private UserAdapter userAdapter;
    private UserMenuAdapter menuAdapter;
    private ImageView itemsImg;
    private TextView mine_name;
    private ImageView menu_head;


    @Override
    public int bindLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView() {
        user_rv = f(R.id.user_rv);
        menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_menu,null);
        mine_name = menuView.findViewById(R.id.mine_name);
        menu_head = menuView.findViewById(R.id.menu_head_img);
        timeLineView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_speed,null);
        menu_rv = menuView.findViewById(R.id.menu_rv);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        menu_rv.setLayoutManager(gridLayoutManager);
        menuAdapter = new UserMenuAdapter();
        menu_rv.setAdapter(menuAdapter);

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        user_rv.setLayoutManager(manager);
        userAdapter = new UserAdapter();
        //添加头布局
        user_rv.setAdapter(userAdapter);
        user_rv.addItemDecoration(new OnLine(getContext()));
        userAdapter.addHeaderView(menuView);
        userAdapter.addHeaderView(timeLineView);
        mine_name.setOnClickListener(this::onClick);
    }

    @Override
    public void initData() {
        p.initData();

    }

    @Override
    public void inject() {
        DaggerUserComponent.builder().userModules(new UserModules(this)).build().inject(this);
    }

    @Override
    public void refresh(BaseEntity baseEntity) {

    }

    @Override
    public void refreshMenu(List<UserMenuEntity> menues) {
        menuAdapter.setNewData(menues);
    }

    @Override
    public void refreshItems(List<UserItemEntity> items) {
        userAdapter.setNewInstance(items);
        userAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                items.get(position).setSelFlag(!items.get(position).isSelFlag());
                userAdapter.notifyDataSetChanged();
                Log.e("", "onItemClick: "+items.get(position).isSelFlag() );
                if (items.get(position).name.equals("更多设置")){
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        List<UserEntity> list = App.getInstance().getDaoSesson().queryBuilder(UserEntity.class).list();
        if (list.size()!=0){
            mine_name.setText(list.get(0).getUser_name());
            Glide.with(getActivity()).load(list.get(0).getUser_img()).transform(new CircleCrop(),new CenterCrop()).into(menu_head);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.mine_name==id){
            List<UserEntity> userEntities = App.getInstance().getDaoSesson().loadAll(UserEntity.class);
            if (userEntities.size()==0){
                startPage(LoginActivity.class);
            }else {
                startPage(PersonActivity.class);
            }
        }
    }
}
